package com.eibus.applicationconnector.dotnet;

import junit.framework.TestCase;
import com.eibus.connector.nom.Connector;
import com.eibus.xml.nom.Node;

//@Author	: Lejil
//@Class	: DotnetReferenceArgumentsErrorTest

/**
 *	<br/>Owner:			Lejil
 *	<br/>Description:	Invoke the .NET public methods with ByRef Arguments, For all the dataTypes with incorrect values 
 */
public class DotnetReferenceArgumentsErrorTestCase extends TestCase
{

	Connector  connector ;
	private final String faultCode	=	"Arguments.Type-Mismatch";
	
	protected void setUp()
	{
		try
		{
			connector = Connector.getInstance("DotNET Client");
			if (!connector.isOpen())
				connector.open();
		}
		catch(Exception ex)
		{
			fail("Could not get instance of Connector. "+ex.getMessage());
		}
	}
	
	protected void tearDown()
	{

	}

	public DotnetReferenceArgumentsErrorTestCase(String name)
	{
		super(name);
	}


	public static void main(String[] args)
	{
		junit.textui.TestRunner.run(DotnetReferenceArgumentsErrorTestCase.class);
	}



	public int  sendRequest(String methodName,String argsOne,String argsTwo)
	{
		int request=0,response=0;
		try
		{
			
			request = connector.createSOAPMethod("http://schemas.cordys.com/1.0/dotnet/RefUnitTest",methodName);
			Node.createTextElement("args1",argsOne,request);
			Node.createTextElement("args2",argsTwo,request);
			response = connector.sendAndWait(Node.getParent(request));
			checkforSoapFault(response);
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method :- " + methodName +" Exception :- " + ex.toString());
		}
		finally
		{
			Util.delete(Node.getRoot(request));
		}
		return response;
	}




	public  boolean checkforSoapFault(int response) throws Exception
	{
		int resultNode=0;
		try
		{
			int  faultNode = XPathUtil.getSOAPFault(response);

			if ( faultNode == 0)
			{
				fail(".NET Connector Expected soapFault, but returned Incorrect result" );
			}
		}
		catch(Exception ex)
		{
			throw ex;
		}
		return true;
	}
    
    private void commonTestMethod(String methodName, String argsOne, String argsTwo, String expectedFaultcode)
    {
        int response =0;
        try
        {
             response =sendRequest(methodName, argsOne, argsTwo);
             //String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
             String result = Node.getDataWithDefault(XPathUtil.getSOAPFaultcode(response), "No SOAP Fault");
             if (!result.equals(expectedFaultcode))
             {
                 //fail(methodName +  " Expected value is "+ expectedFaultcode+", but returned " + result);
                 System.out.println(methodName);
                 System.out.println("Expected value is : " + expectedFaultcode);
                 System.out.println("Result value is : " + result);
             }
             //assertEquals(expectedFaultcode, result);
        }
        catch(Exception ex)
        {
            fail(".NET Connector junit Failed for the .NET Method :- " + methodName + " :: "+ex.toString());
        }
        finally
        {
            Util.delete(Node.getRoot(response));
        }
    }

	public void testrefBooleanArgs() //1
	{
        commonTestMethod("TestBoolType","true","13plse", faultCode);
	}


	public void testrefByteArgs() //2
	{
        commonTestMethod("TestByteType","0","9k", faultCode);
	}

	public void testrefCharArgs() //3
	{
        commonTestMethod("TestCharType","a","er56b", faultCode);
	}
    
	public void testrefDoubleArgs() //4
	{
        commonTestMethod("TestDoubleType","er.34","20.23", faultCode);
	}
    
	public void testrefIntArgs() //5
	{
        commonTestMethod("TestIntegerType","10uip000","28823d8", faultCode);
	}
    
	public void testrefLongArgs() //6
	{
        commonTestMethod("TestLongType","12dfgh1899","12344", faultCode);
	}
	public void testrefSingleArgs() //8
	{
        commonTestMethod("TestSingleType","12a.53","23.5da", faultCode);
	}
	

	/*
	public void testrefStringArgs() //9
	{
		int response =0;
		try
		{
			response = sendRequest("TestStringType","cordys","India");
			String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
			if (!result.equals(faultCode))
			{
				fail("TestStringType Expected value is "+ faultCode+", but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : TestStringType");
		}
		finally
		{
			Node.delete(Node.getRoot(response));
		}
	}*/
	
	
	public void testrefXMLArgs() //10
	{
        commonTestMethod("TestXmlType","12.5","23.5", "Server.NotImplemented");
	}
	
	public void testrefDateTimeArgs() //11
	{
        commonTestMethod("TestDateTimeType","12.5","23.5", faultCode);
	}
	
	public void testrefXMLNomArgs() //12
	{
        commonTestMethod("TestXMLNomType","12.5","23.5", faultCode);
	}
	

	// STATIC METHODS
	//
	
	
	
	public void testrefStaticBooleanArgs() //1
	{
        commonTestMethod("TestStaticBoolType","trsa7ue","false", faultCode);
	}


	public void testrefStaticByteArgs() //2
	{ 
        commonTestMethod("TestStaticByteType","0yu","121", faultCode);
	}

	public void testrefStaticCharArgs() //3
	{
        commonTestMethod("TestStaticCharType","ad1b","ad3b", faultCode);
	}
	public void testrefStaticDoubleArgs() //4
	{
        commonTestMethod("TestStaticDoubleType","1228kk.34","20.23", faultCode);
	}
    
	public void testrefStaticIntArgs() //5
	{
        commonTestMethod("TestStaticIntegerType","11jkk0000","df23", faultCode);
	}
	public void testrefStaticLongArgs() //6
	{
        commonTestMethod("TestStaticLongType","18tyu99","1233qwe44", faultCode);
	}
	public void testrefStaticShortArgs() //7
	{
        commonTestMethod("TestStaticShortType","2.er99","1.ff24", faultCode);
	}
    
	public void testrefStaticSingleArgs() //8
	{
        commonTestMethod("TestStaticSingleType","12i.a..5","23.5", faultCode);
	}
	

	/*
	public void testrefStaticStringArgs() //9
	{
		int response =0;
		try
		{
			response = sendRequest("TestStaticStringType","cordys","India");
			String result = Find.firstDataWithDefault(response,"?<SOAP:faultcode>","CorrectResult");
			if (!result.equals(faultCode))
			{
				fail("TestStaticStringType Expected value is "+ faultCode+", but retrunred " + result);
			}
		}
		catch(Exception ex)
		{
			fail(".NET Connector junit Failed for the .NET Method : TestStaticStringType");
		}
		finally
		{
			Node.delete(Node.getRoot(response));
		}
	}*/
	
	
	public void testrefStaticXMLArgs() //10
	{
        commonTestMethod("TestStaticXmlType","12.5","23.5", "Server.NotImplemented");
	}
	
	public void testrefStaticDateTimeArgs() //11
	{
        commonTestMethod("TestStaticDateTimeType","12.5","23.5", faultCode);
	}
	
	public void testrefStaticXMLNomArgs() //12
	{
        commonTestMethod("TestXMLNomType","12.5","23.5", faultCode);
	}	
}
